/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package quan;

import java.awt.Graphics2D;
import java.awt.Point;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
import javax.swing.JComponent;

/**
 *
 * @author Quannk
 */
public class QUtility {

    public static BufferedImage getImage(JComponent component) {
        BufferedImage bi = new BufferedImage(component.getWidth(), component.getHeight(), BufferedImage.TYPE_INT_RGB);
        Graphics2D g2d = bi.createGraphics();
        component.paint(g2d);
        return bi;
    }

    public static void saveImage(String filename, String type, BufferedImage bi) throws IOException {
        File f = new File(filename);
        ImageIO.write(bi, type, f);
    }

    public static boolean checkSelectedPoint(Point p1, Point p2, Point selected) {
        double d1 = Math.sqrt(Math.pow(p1.x - selected.x, 2) + Math.pow(p1.y - selected.y, 2));
        double d2 = Math.sqrt(Math.pow(p2.x - selected.x, 2) + Math.pow(p2.y - selected.y, 2));
        double d3 = Math.sqrt(Math.pow(p1.x - p2.x, 2) + Math.pow(p1.y - p2.y, 2));
        int a = 0;
        try {
            a = (p1.y - p2.y) / (p1.x - p2.x);
        } catch (Exception e) {
            System.out.println(e);
            return false;
        }
        int b = p1.y - a * p1.x;
        double delta = 0.01;
        Point newPoint;
        if (Math.abs(d1 + d2 - d3) <= delta) {
            return true;
        }
        return false;
    }

    public static boolean comparePoint(Point p1, Point p2) {
        double d = Math.sqrt(Math.pow(p1.x - p2.x, 2) + Math.pow(p1.y - p2.y, 2));
        double delta = 1;
        if (d <= delta) {
            return true;
        }
        return false;

    }
}
